//
//  RowUploadImagesCell.m
//  
//  jhf
//
//  Created by Mac on 2020/8/13.
//  Copyright © 2020 jhfappkf@163.com. All rights reserved.
//

#import "RowUploadImagesCell.h"
#import "JAddImageCV.h"
#import "RowUploadImagesM.h"
#import "JAddImageModel.h"

@interface RowUploadImagesCell ()

@end

@implementation RowUploadImagesCell

- (void)setup {
    [self updateAddImageCV];
    [self.bgV addSubview:self.addImageCV];
    [self.addImageCV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.bgV).insets(UIEdgeInsetsZero);
    }];
    self.addImageCV.isCellClass = true;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)bindTo:(__kindof id<BindProtocol>)model; {
    [super bindTo:model];
    BindModel *bind = (BindModel *)model;
    RowUploadImagesM *info = (RowUploadImagesM *)bind.model;
    
    self.addImageCV.dataArray = info.currentShowDataArray;
    
    self.addImageCV.cellSize = info.cellSize;
    // 是否需要手动移动
    self.addImageCV.needMove = info.needMove;
    // 删除图片是否需要提出来
    self.addImageCV.delActionDrowOut = info.delActionDrowOut;
    // 最多几个
    self.addImageCV.maxNum = info.maxNum;
    
    // cell之间的行间距
    self.addImageCV.minimumLineSpacing = info.minimumLineSpacing;
    // cell之间的列间距
    self.addImageCV.minimumInteritemSpacing = info.minimumInteritemSpacing;
    // 整体边距
    self.addImageCV.sectionInset = info.sectionInset;
    // 删除按钮
    self.addImageCV.deleteImage = info.deleteImage;
    
    // 计算行高
    CGFloat height = info.bgVMarging.top + info.bgVMarging.bottom;
    height += (info.sectionInset.top + info.sectionInset.bottom);
    if (info.numberOfVertail > 0) {
        NSInteger lineNumer = (info.currentShowDataArray.count - 1) / info.numberOfVertail + 1;
        height += (info.minimumLineSpacing * (lineNumer - 1));
        height += (lineNumer * info.cellSize.height);
    }
    if (bind.cellHeight != height) {
        bind.cellHeight = height;
        [bind.action runService:RefreshRowHeight any:bind.action.key];
    };
}


/**
 显示图片
 */
- (void)updateAddImageCV{
    // 默认四个 高度
    self.addImageCV.isCellClass = true;
    @weakify(self)
    [self.addImageCV loadWithIdentity:@"JAddImageCell" cellCellSize:CGSizeMake(120, 120) andBlock:^(NSInteger idx, NSObject *item) {
        @strongify(self)
        if (idx == -1) { // 删除 操作
            [self.addImageCV.dataArray removeObjectAtIndex:[(NSString *)item integerValue]];
        }else if (idx == -2) { // 删除 对象
            [self.addImageCV.dataArray safeRemoveObjectAtIndex:[self.addImageCV.dataArray indexOfObject:item]];
        }
        BindModel *bind = (BindModel *)self.model;
        RowUploadImagesM *info = (RowUploadImagesM *)bind.model;
        info.dataArray = self.addImageCV.dataArray;
        [self.addImageCV updateSource];
    }];
    // 图片左对齐
    self.addImageCV.cellLeft = true;
    [self.addImageCV configDefault];
    self.addImageCV.reloadFrame = ^(NSInteger idx, NSObject * _Nullable value) {
        @strongify(self)
        BindModel *bind = (BindModel *)self.model;
        RowUploadImagesM *info = (RowUploadImagesM *)bind.model;
        info.dataArray = (NSMutableArray <JAddImageModel *> *)value;
        [bind.action runService:ReloadRow any:bind.action.key];
    };
}

//MARK: - getter
- (JAddImageCV *)addImageCV {
    if (!_addImageCV) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 8;
        flowLayout.minimumLineSpacing = 8;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _addImageCV = [[JAddImageCV alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 0) collectionViewLayout:flowLayout];
    }
    return _addImageCV;
}

@end
